﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraPrinting;
using ERP_RORZE.Tool.Reports;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;

namespace ERP_RORZE.Tool.UserControls
{
    public partial class uc_rpt_GridView : DevExpress.XtraEditors.XtraUserControl
    {
        ReportHelper helper = new ReportHelper();
        public string viewCaption = "Report";
        

        public uc_rpt_GridView(int reportName, string caption)
        {
            InitializeComponent();
            BindData(reportName, caption);
        }

        private void BindData(int reportName, string caption)
        {
            switch (reportName)
            {
                case ToolConstants.Configuration.ReportName.FTotalMachineAtFactoryCrosstabBorrow:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FTotalMachineAtFactory_Crosstab_Borrow);
                    gridView1.PopulateColumns();
                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolID" && column.FieldName != "ToolName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }
                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.FLossDetails:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FLossDetails);
                    gridView1.PopulateColumns();

                    GridColumn colQuantiy = gridView1.Columns["Quantity"];
                    colQuantiy.DisplayFormat.FormatType = FormatType.Numeric;
                    colQuantiy.DisplayFormat.FormatString = "n0";

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.FTonKhoTongHop:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FTonKhoTongHop);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "Price")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                        else if (column.FieldName != "ToolID" && column.FieldName != "ToolName" && column.FieldName != "FrequencyID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.FDailyBroken:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FDailyBroken);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "PriceJPY" || column.FieldName == "ThanhTienJPY" )
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                        else if (column.FieldName == "TongSoLuong")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBroken2CrosstabAmountUSD:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken2_CrosstabAmount_USD);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolID" && column.FieldName != "ToolName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBroken2CrosstabQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken2_CrosstabQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolID" && column.FieldName == "ToolName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMachineCrosstabAmountUSD:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Machine_CrosstabAmount_USD);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolGroupName" && column.FieldName != "MachineID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMachineCrosstabQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Machine_CrosstabQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolGroupName" && column.FieldName == "MachineID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenCustomer2CrosstabQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Customer2_CrosstabQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolGroupName" && column.FieldName == "CustomerID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenCustomer2CrosstabAmountUSD:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Customer2_CrosstabAmount_USD);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolGroupName" && column.FieldName != "CustomerID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBroken3CrosstabToolGroupAmount:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken3_CrosstabToolGroupAmount);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolGroupName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBroken3CrosstabToolGroupQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken3_Crosstab_ToolGroupQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolGroupName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMaterial2CrosstabAmountUSD:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Material2_CrosstabAmount_USD);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolGroupName" && column.FieldName != "MaterialID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMaterial2CrosstabQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Material2_CrosstabQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolGroupName" && column.FieldName == "MaterialID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMistake2CrosstabAmountUSD:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Mistake2_CrosstabAmount_USD);

                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolGroupName" && column.FieldName != "MistakeID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "c2";
                        }
                    }
                    
                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QMonthlyBrokenMistake2CrosstabQty:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Mistake2_CrosstabQty);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName == "ToolGroupName" && column.FieldName == "MistakeID")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;
                case ToolConstants.Configuration.ReportName.QTotalMachineBrokenCrosstab:
                    gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QTotalMachineBroken_Crosstab);
                    gridView1.PopulateColumns();

                    foreach (GridColumn column in gridView1.Columns)
                    {
                        if (column.FieldName != "ToolID" && column.FieldName != "ToolNamw")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n0";
                        }
                    }
                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ViewCaption = caption;
                    break;

            }

        }

        private void btExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.RowCount > 0)
            {
                XlsxExportOptions option = new XlsxExportOptions();
                option.ShowGridLines = true;
                option.SheetName = gridView1.ViewCaption;

                FileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
                fileDialog.FileName = gridView1.ViewCaption;
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    //gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ExportToXlsx(fileDialog.FileName, option);
                }
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.NoDataToExport, ToolConstants.Form.MessageBox.Message,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}
